import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import store from '@/store/store.js'
import route from '@/router'


// import store from '@/store'
// import { getToken } from '@/utils/auth'

// create an axios instance
//刷新页面获取用户信息
window.onbeforeunload = function () {
    if (sessionStorage.getItem("userInfo")) {
        store.state.userInfo = JSON.parse(sessionStorage.getItem("userInfo"))
    }
};
window.onbeforeunload()
//请求接口axios
const service = axios.create({
    // baseURL: "http://fndevpcserver.yuwann.com/orangecatpc100/",//手机验证。。。

     baseURL: 'https://fndevpcserver.yuwann.com/orangecatpc100/',//
    // baseURL: "https://pc.fishnovel.com/orangecatpc100",//线上 发版
    headers: {
        // 'Content-Type': 'application/x-www-form-urlencoded',
        // 'authorization': store.state.userInfo.token ? store.state.userInfo.token : '', //VueX
        'authorization': localStorage.getItem("token")
    },
    timeout: 5000 // request timeout
})

/**
 * 请求前
 * 请求拦截器
 */
service.interceptors.request.use(
    config => {
        // do something before request is sent
        if (!config.headers.hasOwnProperty('Authorization')) {
            config.headers.authorization = store.state.userInfo.token
        }
        return config
    },
    error => {
        // do something with request error
        console.log(error) // for debug
        return Promise.reject(error)
    }
)
/**
 * 请求后
 * 响应拦截器
 */
service.interceptors.response.use(
    response => {
        const res = response.data
        if (res.code == 200018) {
            Message({
                message: '作者笔名超出长度限制2-10 个字',
                type: 'error',
                duration: 3 * 1000
            })
        }
        if (res.code == 200003) {
            Message({
                message: '请选择书籍分类',
                type: 'error',
                duration: 3 * 1000
            })
        }
        if (res.code == 100012) {
            Message({
                message: '章节字数不足500字，或者字数超出50000字',
                type: 'error',
                duration: 3 * 1000
            })
        }
        if (res.code == 100015) {
            Message({
                message: '章节审核中不能修改',
                type: 'error',
                duration: 3 * 1000
            })
        }
        if (res.code == 40003) {
            Message({
                message: res.message || '未登录',
                type: 'error',
                duration: 3 * 1000
            })
            route.push('/home')
            store.state.userInfo = {};
            sessionStorage.removeItem("userInfo");
        }
        if (res.code == 40005) {
            Message({
                message: res.message || '登录超时',
                type: 'error',
                duration: 3 * 1000
            })
            route.push('/home')
            store.state.userInfo = {};
            sessionStorage.removeItem("userInfo");
        }
        if (res.code == 200028) {
            Message({
                message: '地址包含敏感词',
                type: 'error',
                duration: 3 * 1000
            })
        }
        if (res.code == 200029) {
            Message({
                message: '简介包含敏感词',
                type: 'error',
                duration: 3 * 1000
            })
        }
        if (res.code == 200027) {
            Message({
                message: '姓名包含敏感词',
                type: 'error',
                duration: 3 * 1000
            })
        }
        if (res.code == 200026) {
            Message({
                message: '笔名名包含敏感词',
                type: 'error',
                duration: 3 * 1000
            })
        }
        if (res.code == 200024) { //100016
            Message({
                message: 'User name already exists',
                type: 'error',
                duration: 3 * 1000
            })
        }
        if (res.code == 100016) { //
            Message({
                message: '内容包含敏感词汇',
                type: 'error',
                duration: 3 * 1000
            })
        }
        if (res.code == 200015) { //
            Message({
                message: 'Verification code error',
                type: 'error',
                duration: 3 * 1000
            })
        }
        if (res.code == 200014) { //
            Message({
                message: '手机号已经注册',
                type: 'error',
                duration: 3 * 1000
            })
        }

        return res
    },
    error => {
        // Message({
        //   message: error.message,
        //   type: 'error',
        //   duration: 5 * 1000
        // })
        // return Promise.reject(error)
    }
)

export default service